package com.nz.net.hays.www;


import java.util.ArrayList;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.StrictMode;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class SignUp extends Activity {
	Button login,SignUp;
	private EditText username,password,emailid;
	private ProgressDialog pDialog;
	int flag=0;
	JSONParser jsonParser = new JSONParser();
	//private static String url = "http://10.0.2.2/rdias/register.php";
	private static String url = "http://hays.byethost6.com/haysregister.php";
	private static final String TAG_SUCCESS = "success"; 
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
		.detectDiskReads().detectDiskWrites().detectNetwork()
		.penaltyLog().build());
		super.onCreate(savedInstanceState);
		setContentView(R.layout.signup);
	
	//Go To Login.java	
//		login=(Button)findViewById(R.id.login);	
//        login.setOnClickListener(new View.OnClickListener() 
//        {		
//			@Override
//			public void onClick(View view) {
//				Intent i = new Intent(getApplicationContext(), MainActivity.class);
//				startActivity(i);
//				
//			}
//		}); 
    // Close Login.java
        
     //Get all data and log in 
    	SignUp=(Button)findViewById(R.id.SignUp);	
    	username=(EditText)findViewById(R.id.username);
    	password=(EditText)findViewById(R.id.password);
    	//hint=(EditText)findViewById(R.id.hint);
    	emailid=(EditText)findViewById(R.id.email_id);
    	
        SignUp.setOnClickListener(new View.OnClickListener() 
        {			
			@Override
			public void onClick(View view) {
				
		//Check all fields		
				if(username.length()<10)
				{
					Toast.makeText(SignUp.this,"Please Enter correct mobile number", Toast.LENGTH_LONG).show();
					return;
				}
				 if(password.length()<4)
				{				
					Toast.makeText(SignUp.this,"Please Enter minimum 4 letters in password", Toast.LENGTH_LONG).show();
					return;
				}
//				 if(hint.length()<4)
//					{				
//						Toast.makeText(SignUp.this,"Please Enter minimum 4 leters in hint", Toast.LENGTH_LONG).show();
//						return;
//					} 
				 if(emailid.length()<10)
					{				
						Toast.makeText(SignUp.this,"Please Enter correct email id", Toast.LENGTH_LONG).show();
						return;
					}
		//check connectivity		
				 if(!isOnline(SignUp.this))
				{					
					Toast.makeText(SignUp.this,"No network connection", Toast.LENGTH_LONG).show();
					return;	
				}
		
		//from login.java		
					new loginAccess().execute();
			}
   
		//code to check online details
		private boolean isOnline(Context mContext) {
		ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo netInfo = cm.getActiveNetworkInfo();
		if (netInfo != null && netInfo.isConnectedOrConnecting())
	   	  {
			return true;
     		}
		    return false;
       	}
      //Close code that check online details		
	  }); 
        //Close log in 
    }


class loginAccess extends AsyncTask<String, String, String> {

	private	String user;
	private	String pwd;
	protected void onPreExecute() {
		super.onPreExecute();
		pDialog = new ProgressDialog(SignUp.this);
		pDialog.setMessage("Signing up...");
		pDialog.setIndeterminate(false);
		pDialog.setCancelable(true);
		pDialog.show();
	}
	@Override
	protected String doInBackground(String... arg0) {
		List<NameValuePair> params = new ArrayList<NameValuePair>();
		 user=username.getText().toString();
		 pwd=password.getText().toString();
		//String h=hint.getText().toString();
		String id=emailid.getText().toString();
		
		params.add(new BasicNameValuePair("username",user));
		params.add(new BasicNameValuePair("password", pwd));
		//params.add(new BasicNameValuePair("hint", h));
		params.add(new BasicNameValuePair("emailid", id));
		

		JSONObject json = jsonParser.makeHttpRequest(url,"POST", params);
		
		Log.d("Create Response", json.toString());
		
		try {
			int success = json.getInt(TAG_SUCCESS);
			if (success == 1) 
			 {
			  flag=0;	
			 
			
			 }
			 else
			 {
				// failed to Sign in
				flag=1;
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}
		return null;
	}
	protected void onPostExecute(String file_url) {
		pDialog.dismiss();
		if(flag==0)
		{
			 Intent i = new Intent(SignUp.this,WelcomeWithDrawerActivity.class);
			 i.putExtra("username",user);
			  i.putExtra("password",pwd);
			  startActivity(i);
			  finish();
		}
		if(flag==1)
		{
			Toast.makeText(SignUp.this,"Please Enter Correct informations", Toast.LENGTH_LONG).show();
			
		}
			
	}

	
  }
	
}
